package edu.wayne.cs.csc4110.hw6.framework.message;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import edu.wayne.cs.csc4110.hw6.interfaces.message.Messages;
import edu.wayne.cs.csc4110.hw6.interfaces.message.PrivateMessage;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class MessageServer implements Messages {

	private static MessageServer server = null;
	private HashMap<User, List<PrivateMessage>> privateStorage;

	public MessageServer() {
		privateStorage = new HashMap<User, List<PrivateMessage>>();
	}

	@Override
	public List<PrivateMessage> getMessages(User user) {
		if (privateStorage.containsKey(user))
			return privateStorage.get(user);

		return null;

	}

	@Override
	public boolean storeMessage(PrivateMessage msg) {
		System.out.println(msg.getFormatedMessage());
		if (!privateStorage.containsKey(msg.getRecipient())) {
			privateStorage.put(msg.getRecipient(),
					new ArrayList<PrivateMessage>());
		}
		if (!privateStorage.containsKey(msg.getSender())) {
			privateStorage
					.put(msg.getSender(), new ArrayList<PrivateMessage>());
		}
		return privateStorage.get(msg.getRecipient()).add(msg);

	}

	public static MessageServer getInstance() {
		if (server == null) {
			server = new MessageServer();
		}
		return server;
	}

}
